<cfcomponent>

<cffunction name="parseMember" access="package" returntype="struct">
	<cfargument name="memberData" type="query" required="Yes" />
	<cfargument name="i" type="numeric" required="No" default="1" />
	<cfset var member = structNew() />
	<cfset member.id = arguments.memberData.memberId[i] />
	<cfset member.emailAddress = arguments.memberData.emailAddress[i] />
	<cfset member.firstName = arguments.memberData.firstName[i] />
	<cfset member.lastName = arguments.memberData.lastName[i] />
	<cfset member.company = arguments.memberData.company[i] />
	<cfset member.title = arguments.memberData.title[i] />
	<cfset member.bio = arguments.memberData.bio[i] />
	<cfset member.isApproved = arguments.memberData.isApproved[i] />
	<cfset member.isAdmin = arguments.memberData.isAdmin[i] />
	<cfset member.messageCount = arguments.memberData.messageCount[i] />
   	<cfif isDefined("arguments.memberData.endorsementReceivedCount")>
		<cfset member.endorsementReceivedCount = arguments.memberData.endorsementReceivedCount[i] />
    </cfif>
    <cfif isDefined("arguments.memberData.endorsementGivenCount")>
		<cfset member.endorsementGivenCount = arguments.memberData.endorsementGivenCount[i] />
    </cfif>
    <cfif isDefined("arguments.memberData.pendingEndorsementCount")>
		<cfset member.pendingEndorsementCount = arguments.memberData.pendingEndorsementCount[i] />
    </cfif>
	<cfset member.dateAdded = arguments.memberData.dateAdded[i] />
	<cfreturn member />
</cffunction>

<cffunction name="parseEndorsement" access="package" returntype="struct">
	<cfargument name="endorsementData" type="query" required="Yes" />
	<cfargument name="i" type="numeric" required="No" default="1" />
	<cfset var endorsement = structNew() />
	<cfset endorsement.id = arguments.endorsementData.memberEndorsementId[i] />
    <cfset endorsement.memberId = arguments.endorsementData.memberId[i] />
    <cfset endorsement.authorName = arguments.endorsementData.authorName[i] />
    <cfset endorsement.rank = arguments.endorsementData.rank[i] />
    <cfset endorsement.message = arguments.endorsementData.message[i] />
    <cfset endorsement.isApproved = arguments.endorsementData.isApproved[i] />
    <cfset endorsement.dateAdded = arguments.endorsementData.dateAdded[i] />
	<cfreturn endorsement />
</cffunction>

<cffunction name="getMembers" access="remote" returntype="array">
	<cfquery name="getMembers" datasource="#application.datasource#">
		EXEC getMembers;
	</cfquery>
	<cfset members = arrayNew(1) />
	<cfloop from="1" to="#getMembers.recordCount#" index="i">
		<cfset members[i] = this.parseMember(getMembers, i) />
	</cfloop>
	<cfreturn members />
</cffunction>

<cffunction name="getPendingMembers" access="remote" returntype="array">
	<cfquery name="getPendingMembers" datasource="#application.datasource#">
		EXEC getPendingMembers;
	</cfquery>
	<cfset pendingMembers = arrayNew(1) />
	<cfloop from="1" to="#getPendingMembers.recordCount#" index="i">
		<cfset pendingMembers[i] = this.parseMember(getPendingMembers, i) />
	</cfloop>
	<cfreturn pendingMembers />
</cffunction>

<cffunction name="setEndorsement" access="remote">
	<cfargument name="endorsement" type="struct" required="Yes" />
	<cfargument name="dummy" type="string" required="No" />
	<cfquery name="setEndorsement" datasource="#application.datasource#">
		EXEC setEndorsement <cfif isDefined("arguments.endorsement.id")>#arguments.endorsement.id#<cfelse>NULL</cfif>, #arguments.endorsement.memberId#, #arguments.endorsement.authorId#, #arguments.endorsement.rank#, '#arguments.endorsement.message#';
	</cfquery>
    <!--- Send User an email saying they have a new endorsement --->
</cffunction>

<cffunction name="getEndorsements" access="remote" returntype="array">
	<cfargument name="memberId" type="numeric" required="Yes" />
	<cfquery name="getEndorsements" datasource="#application.datasource#">
		EXEC getEndorsements #arguments.memberId#;
	</cfquery>
	<cfset endorsements = arrayNew(1) />
	<cfloop from="1" to="#getEndorsements.recordCount#" index="i">
		<cfset endorsements[i] = this.parseEndorsement(getEndorsements, i) />
	</cfloop>
	<cfreturn endorsements />
</cffunction>

<cffunction name="getPendingEndorsements" access="remote" returntype="array">
	<cfargument name="memberId" type="numeric" required="Yes" />
	<cfquery name="getPendingEndorsements" datasource="#application.datasource#">
		EXEC getPendingEndorsements #arguments.memberId#;
	</cfquery>
	<cfset pendingEndorsements = arrayNew(1) />
	<cfloop from="1" to="#getPendingEndorsements.recordCount#" index="i">
    	<cfset pendingEndorsements[i] = this.parseEndorsement(getPendingEndorsements, i) />
	</cfloop>
	<cfreturn pendingEndorsements />
</cffunction>

<cffunction name="getReputation" access="remote" returntype="numeric">
	<cfif not isDefined("session.member")>
		<cfthrow message="Member is not currently logged in." />
	</cfif>
    <!--- TODO: Get Reputation --->
	<cfreturn 0 />
</cffunction>

<cffunction name="getActivityRating" access="remote" returntype="numeric">
	<cfif not isDefined("session.member")>
		<cfthrow message="Member is not currently logged in." />
	</cfif>
    <!--- TODO: Get Activity Rating --->
	<cfreturn 0 />
</cffunction>

<cffunction name="setMember" access="remote">
	<cfargument name="member" type="struct" required="Yes" />
	<cfargument name="oldPassword" type="string" required="Yes" />
	<cfif not isDefined("session.member")>
		<cfthrow message="Member is not currently logged in." />
	</cfif>
	<cfquery name="getMember" datasource="#application.datasource#">
		EXEC getMember '#session.member.emailAddress#', NULL;
	</cfquery>
	<cfif lCase(getMember.password) neq lCase(arguments.oldPassword)>
		<cfthrow message="The password provided is not correct." />
	</cfif>
	<cfquery name="setMember" datasource="#application.datasource#">
		EXEC setMember '#arguments.member.id#', '#arguments.member.emailAddress#', <cfif not isDefined("arguments.member.password") or arguments.member.password eq ''>NULL<cfelse>'#arguments.member.password#'</cfif>, '#arguments.member.firstName#', '#arguments.member.lastName#', '#arguments.member.company#', '#arguments.member.title#', '#arguments.member.bio#';
	</cfquery>
	<cfset session.member = arguments.member />
</cffunction>

<cffunction name="getMember" access="remote" returntype="struct">
	<cfargument name="emailAddress" type="string" required="No" />
	<cfargument name="password" type="string" required="No" />
	<cfif isDefined("arguments.emailAddress")>
		<cfquery name="getMember" datasource="#application.datasource#">
			EXEC getMember '#arguments.emailAddress#', <cfif isDefined("arguments.password")>'#arguments.password#'<cfelse>NULL</cfif>;
		</cfquery>
		<cfif getMember.recordCount eq 0>
			<cfthrow type="MEMBER_NOT_FOUND" message="Member not found." />
		</cfif>
		<cfreturn this.parseMember(getMember) />
	<cfelse>
		<cfif not isDefined("session.member")>
			<cfthrow message="Member is not currently logged in." />
		</cfif>
		<cfreturn session.member />
	</cfif>
</cffunction>

<cffunction name="register" access="remote" returntype="struct">
	<cfargument name="member" type="struct" required="Yes" />
	<cfargument name="dummy" type="string" required="No" />
	<cftry>
		<cfset member = this.getMember(arguments.member.emailAddress) />
		<cfcatch type="MEMBER_NOT_FOUND">
			<cfquery name="setMember" datasource="#application.datasource#">
				EXEC setMember NULL, '#arguments.member.emailAddress#', '#arguments.member.password#', '#arguments.member.firstName#', '#arguments.member.lastName#', '#arguments.member.company#', '#arguments.member.title#', '#arguments.member.bio#';
			</cfquery>
			<cfset session.member = this.getMember(arguments.member.emailAddress) />
			<cfreturn session.member />
		</cfcatch>
	</cftry>
	<cfthrow message="Someone has already registered with this email address." />
</cffunction>

<cffunction name="approveMember" access="remote">
	<cfargument name="memberId" type="numeric" required="Yes" />
    <cfif session.member.isAdmin>
        <cfquery name="approveMember" datasource="#application.datasource#">
            EXEC approveMember #arguments.memberId#;
        </cfquery>
        <!--- TODO: Send User an email saying they were approved. --->
    <cfelse>
    	<cfthrow message="This user does not have access to approve members." />
    </cfif>
</cffunction>

<cffunction name="approveEndorsement" access="remote">
	<cfargument name="endorsementId" type="numeric" required="Yes" />
    <cfquery name="approveEndorsement" datasource="#application.datasource#">
            EXEC approveEndorsement #arguments.endorsementId#;
    </cfquery>
    <cfset session.member.pendingEndorsementCount = session.member.pendingEndorsementCount - 1 />
    <!--- TODO: Send User an email saying their Endorsement was approved. --->
</cffunction>

<cffunction name="login" access="remote" returntype="struct">
	<cfargument name="emailAddress" type="string" required="Yes" />
	<cfargument name="password" type="string" required="Yes" />
	<cfset session.member = getMember(arguments.emailAddress, arguments.password) />
	<cfreturn session.member />
</cffunction>

<cffunction name="logout" access="remote">
	<cfset structDelete(session, "member") />
</cffunction>

</cfcomponent>